package com.ruoyi.web.controller.race;

import java.util.List;
import java.util.Arrays;
import jakarta.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.live.configuration.SessionInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.live.base.domain.LiveExam;
import com.ruoyi.live.base.service.ILiveExamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 比赛Controller
 *
 * @author ruoyi
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/race/competition")
public class LiveExamController extends BaseController
{
    @Autowired
    private ILiveExamService liveExamService;

    /**
     * 查询比赛列表
     */
    @PreAuthorize("@ss.hasPermi('race:comptition:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveExam liveExam)
    {
        startPage();
        QueryWrapper<LiveExam> wrapper = new QueryWrapper<>(liveExam);
        List<LiveExam> list = liveExamService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出比赛列表
     */
    @PreAuthorize("@ss.hasPermi('race:comptition:export')")
    @Log(title = "比赛", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LiveExam liveExam)
    {
        QueryWrapper<LiveExam> wrapper = new QueryWrapper<>(liveExam);
        List<LiveExam> list = liveExamService.list(wrapper);
        ExcelUtil<LiveExam> util = new ExcelUtil<LiveExam>(LiveExam.class);
        util.exportExcel(response, list, "比赛数据");
    }

    /**
     * 获取比赛详细信息
     */
    @PreAuthorize("@ss.hasPermi('race:comptition:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(liveExamService.getById(id));
    }

    /**
     * 新增比赛
     */
    @PreAuthorize("@ss.hasPermi('race:comptition:add')")
    @Log(title = "比赛", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveExam entity)
    {
        entity.setCreateTime(System.currentTimeMillis());
        entity.setCreateUser(SessionInfo.getUserId());
        entity.setState(1);
        return toAjax(liveExamService.save(entity));
    }

    /**
     * 修改比赛
     */
    @PreAuthorize("@ss.hasPermi('race:comptition:edit')")
    @Log(title = "比赛", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveExam liveExam)
    {
        return toAjax(liveExamService.updateById(liveExam));
    }

    /**
     * 设置主赛道
     * @param liveExam
     * @return
     */
    @PreAuthorize("@ss.hasPermi('race:comptition:edit')")
    @Log(title = "比赛", businessType = BusinessType.UPDATE)
    @PutMapping("/setMainRace")
    public AjaxResult setMainRace(@RequestBody LiveExam liveExam)
    {
        liveExamService.lambdaUpdate().eq(LiveExam::getState, 1).set(LiveExam:: getState, 2).update();

        LambdaUpdateWrapper<LiveExam> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LiveExam::getState, 1)
                .set(LiveExam:: getState, 2);
        liveExamService.update(null, wrapper);

        liveExam.setState(1);
        return toAjax(liveExamService.updateById(liveExam));
    }

    /**
     * 删除比赛
     */
    @PreAuthorize("@ss.hasPermi('race:comptition:remove')")
    @Log(title = "比赛", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        return toAjax(liveExamService.removeByIds(list));
    }
}
